package day09.test2;

class Ticket1 extends Thread {
    private int num;

    public Ticket1() {
        this.num = 200;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (this.num <= 0) {
                    break;
                }
                this.num--;
                System.out.println("现在是" + Thread.currentThread().getName() + "正在售票,还剩" + this.num + "张");
                try {
                    // 通知其他线程
                    notifyAll();
                    // 等待其他线程操作
                    if (this.num > 0) {
                        wait();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

public class test_synchronized_no_static {
    public static void main(String[] args) {
        Ticket1 ticket = new Ticket1();

        Thread thread1 = new Thread(ticket, "售票线程1");
        Thread thread2 = new Thread(ticket, "售票线程2");
        Thread thread3 = new Thread(ticket, "售票线程3");

        thread1.start();
        thread2.start();
        thread3.start();

    }
}